package com.easy.easy.Controller;

import com.easy.easy.Service.IProductService;
import com.easy.easy.bean.Category;
import com.easy.easy.bean.Product;
import com.easy.easy.util.LayData;
import com.easy.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    IProductService productService;
    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text) {
        List<Product> list=productService.getList(page,text);
        int count=productService.getCount(text);
        return new LayData(count,list);
    }



    @RequestMapping("getByID")
    @ResponseBody
    public LayData getList(Integer id){
        Product product=productService.getByID(id);
        return LayData.success(product);
    }




    @RequestMapping("delete")
    @ResponseBody
    public int delete(int id) {
        return productService.delete(id);
    }




    @RequestMapping("add")
    @ResponseBody
    public int add(Product product) {
        return productService.add(product);
    }

    @RequestMapping("edit")
    @ResponseBody
    public int edit(Product product) {
        return productService.edit(product);
    }

    @RequestMapping("getChildren")
    @ResponseBody
    public List<Category> getChildren(Integer id) {
        return productService.getChildren(id);
    }

    @RequestMapping("getParent")
    @ResponseBody
    public List<Category> getParent(Integer id) {
        return productService.getParent(id);
    }

}
